package oop.core.map;

/**
 * A class to encapsulate the location of something on the map. 
 */
public class Location implements Cloneable{
	private int xPos;
	private int yPos;

	/**
	 * @param xPos
	 * @param yPos
	 */
	public Location(int xPos, int yPos) {
		this.xPos = xPos;
		this.yPos = yPos;
	}
	
	public Location(Location l){
		this.xPos = l.xPos;
		this.yPos = l.yPos;
	}

	public int getXPos(){
		return xPos;
	}
	
	public int getYPos(){
		return yPos;
	}
	
	public void setLocation(int x, int y){
		xPos = x;
		yPos = y;
	}
	
	public static Location add(Location a, Location b){
		return new Location(a.xPos + b.xPos,a.yPos + b.yPos);
	}
	
	public static int distance(Location a, Location b){
		return (int)(Math.sqrt(Math.pow(a.xPos - b.xPos,2) + Math.pow(a.yPos - b.yPos, 2)) + .5);
	}
	
	public Location clone(){
		try{
		      Location cloned = (Location)super.clone();
		      return cloned;
		} catch(CloneNotSupportedException e){
		      System.out.println(e);
		      return null;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + xPos;
		result = prime * result + yPos;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Location other = (Location) obj;
		if (xPos != other.xPos)
			return false;
		if (yPos != other.yPos)
			return false;
		return true;
	}	
	
	public String toString(){
		return "( " + xPos + ", " + yPos + " )";
	}
}

